﻿using Coldairarrow.Business;
using Coldairarrow.Entity;
using Coldairarrow.Util;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;

namespace Coldairarrow.Api.Controllers.Orderweb
{
    [Route("api/[controller]/[action]")]




    [ApiController]
    public class ShopCartController : BaseApiController
    {
        public ShopCartController(IShop_CartBusiness CartBusiness, IOrder_DecimalBusiness order_DecimalBus, IOrder_MasterBusiness order_MasterBus)
        {
            _CartBusiness = CartBusiness;
            _order_DecimalBus = order_DecimalBus;
            _order_MasterBus = order_MasterBus;
        }

        private IOrder_DecimalBusiness _order_DecimalBus { get; }
        private IOrder_MasterBusiness _order_MasterBus { get; }
        private IShop_CartBusiness _CartBusiness { get; }

        /// <summary>
        /// 添加购物车
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task AddToShopCart(List<Shop_Cart> data)
        {
            await _CartBusiness.AddDataTwoAsync(data);
        }

        /// <summary>
        /// 更新数量
        /// </summary>
        /// <param name="cartNum"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task UpdateNum(ShopCartNum cartNum)
        {
            await _CartBusiness.UpdateNum(cartNum);
        }

        /// <summary>
        /// 获取购物车
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<Shop_CartDtos> GetShopCart(PageInput<ShopCartd> shopCartd)
        {
            return await _CartBusiness.GetShopCart(shopCartd);
        }

        /// <summary>
        /// 测试
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [NoCheckJWT]
        public Order Test(string Id,Int32 seconds,string key,string value)
        {
            //var csredis = new CSRedis.CSRedisClient("127.0.0.1:6379");

            Order order = new Order();
          
            if (!string.IsNullOrWhiteSpace(Id))
            {
                var t = RedisHelper.Ttl(Id);
                order.ttl = t;
                
            }
            if (!string.IsNullOrWhiteSpace(key))
            {
                RedisHelper.Set(key, value, seconds);
                
            }
            
            //sub.Subscribe("__keyevent@0__:expired", (channel, message) =>
            //{
            //    Console.WriteLine("接受到发布的内容为：" + message);(key, msg => action(msg.Body)));
            //});
           
            {
                
               

            };
           // RedisHelper.SubscribeList("__keyevent@0__:expired", (msg) => { Console.WriteLine("测试1" + msg); });
            //RedisHelper.Subscribe(("__keyevent@0__:expired", msg => System.Console.WriteLine(msg.Body)));
            return order;
        }


        [NoCheckJWT]
        [HttpPost]
        public IActionResult TestTwo(A a)
        {
            return Ok();
        }

        public class A
        {
            [Required(ErrorMessage = "{0}是必须的")]
            public string name { get; set; }
            [MaxLength(10,ErrorMessage = "{0}是必须的")]
            public string Id { get; set; }
        }

        public class Order
        {
            public string Id { get; set; }

            public long ttl { get; set; }
        }
    }
}